package com.slofzx.nuoda.evaluation.param.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.slofzx.nuoda.common.enums.CommonResultEnum;
import com.slofzx.nuoda.common.enums.ServiceResult;
import com.slofzx.nuoda.common.service.impl.BaseServiceImpl;
import com.slofzx.nuoda.common.util.IdGen;
import com.slofzx.nuoda.evaluation.param.dao.CommonUnitCostDAO;
import com.slofzx.nuoda.evaluation.param.dto.CommonFixedDTO;
import com.slofzx.nuoda.evaluation.param.dto.CommonUnitCostDTO;
import com.slofzx.nuoda.evaluation.param.entity.CommonFixed;
import com.slofzx.nuoda.evaluation.param.entity.CommonUnitCost;
import com.slofzx.nuoda.evaluation.param.entity.CommonYearRelated;
import com.slofzx.nuoda.evaluation.param.service.CommonUnitCostService;
import com.slofzx.nuoda.evaluation.param.vo.CommonUnitCostVO;
import com.slofzx.nuoda.evaluation.project.util.BusiUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公共单位成本参数服务实现类
 *
 * @author nuoda
 * @since 2023-10-17 16:02:46
 */
@Service("commonUnitCostService")
public class CommonUnitCostServiceImpl extends BaseServiceImpl implements CommonUnitCostService {

    @Resource
    private CommonUnitCostDAO commonUnitCostDAO;

    /**
     * 将数据表数据转换为返回数据类型
     *
     * @param entity 数据表对应类
     * @return 实例对象
     */
    protected CommonUnitCostVO parseEntity2VO(CommonUnitCost entity) {
        CommonUnitCostVO vo = new CommonUnitCostVO();
        vo.setId(entity.getId());
        vo.setVersionId(entity.getVersionId());
        vo.setCompanyId(entity.getCompanyId());
        vo.setOperateCostStandard(entity.getOperateCostStandard());
        vo.setMiningOperationPrice(entity.getMiningOperationPrice());
        vo.setWaterInjectionPrice(entity.getWaterInjectionPrice());
        vo.setHeavyOilThRecPrice(entity.getHeavyOilThRecPrice());
        vo.setOilGasProcessPrice(entity.getOilGasProcessPrice());
        vo.setCondensateRecyclePrice(entity.getCondensateRecyclePrice());
        vo.setUnderWellOperationPrice(entity.getUnderWellOperationPrice());
        vo.setWellTestPrice(entity.getWellTestPrice());
        vo.setGasPurificationPrice(entity.getGasPurificationPrice());
        vo.setRepairPrice(entity.getRepairPrice());
        vo.setTransferPrice(entity.getTransferPrice());
        vo.setOtherAuxiliaryPrice(entity.getOtherAuxiliaryPrice());
        vo.setFactoryMineManagePrice(entity.getFactoryMineManagePrice());
        vo.setCreateUser(entity.getCreateUser());
        vo.setCreateTime(entity.getCreateTime());
        vo.setOilQuotaStandard(entity.getOilQuotaStandard());
        vo.setFixedCost(entity.getFixedCost());
        vo.setVariableCost(entity.getVariableCost());
        return vo;
    }

    /**
     * 将数据传递类转换为数据表数据类
     *
     * @param dto 数据传递类实例
     * @return 实例对象
     */
    protected CommonUnitCost parseDTO2Entity(CommonUnitCostDTO dto) {
        CommonUnitCost entity = new CommonUnitCost();
        entity.setId(dto.getId());
        entity.setVersionId(dto.getVersionId());
        entity.setCompanyId(dto.getCompanyId());
        entity.setOperateCostStandard(dto.getOperateCostStandard());
        entity.setMiningOperationPrice(dto.getMiningOperationPrice());
        entity.setWaterInjectionPrice(dto.getWaterInjectionPrice());
        entity.setHeavyOilThRecPrice(dto.getHeavyOilThRecPrice());
        entity.setOilGasProcessPrice(dto.getOilGasProcessPrice());
        entity.setCondensateRecyclePrice(dto.getCondensateRecyclePrice());
        entity.setUnderWellOperationPrice(dto.getUnderWellOperationPrice());
        entity.setWellTestPrice(dto.getWellTestPrice());
        entity.setGasPurificationPrice(dto.getGasPurificationPrice());
        entity.setRepairPrice(dto.getRepairPrice());
        entity.setTransferPrice(dto.getTransferPrice());
        entity.setOtherAuxiliaryPrice(dto.getOtherAuxiliaryPrice());
        entity.setFactoryMineManagePrice(dto.getFactoryMineManagePrice());
        entity.setOilQuotaStandard(dto.getOilQuotaStandard());
        entity.setFixedCost(dto.getFixedCost());
        entity.setVariableCost(dto.getVariableCost());
        return entity;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CommonUnitCostVO queryById(String id) {
        CommonUnitCost entity = this.commonUnitCostDAO.selectById(id);
        if (entity == null) {
            return null;
        }
        return parseEntity2VO(entity);
    }

    /**
     * 查询多条数据
     *
     * @param dto 数据传递类实例对象
     * @return 对象列表
     */
    @Override
    public List<CommonUnitCostVO> queryList(CommonUnitCostDTO dto) {
        //mybatis-plus 写法示例
        //IPage<CommonUnitCost> pageResult = this.evaModelDAO.selectPage(new Page<>(dto.getPageNo(),dto.getPageSize()),null);
        //List<CommonUnitCost> dataList = pageResult.getRecords();
        //dto.setCount((int) pageResult.getTotal());
        List<CommonUnitCost> dataList = this.commonUnitCostDAO.queryList(dto);
        if (null == dataList || dataList.isEmpty()) {
            return Collections.emptyList();
        }
        List<CommonUnitCostVO> resultList = new ArrayList(dataList.size());
        for (CommonUnitCost item : dataList) {
            CommonUnitCostVO vo = parseEntity2VO(item);
            resultList.add(vo);
        }
        // TODO 添加其他关联查询数据
        return resultList;
    }

    /**
     * 新增数据
     *
     * @param dto 实例对象
     * @return 操作结果
     */
    @Override
    public ServiceResult create(CommonUnitCostDTO dto) {
        // TODO 添加校验逻辑
        CommonUnitCost entity = parseDTO2Entity(dto);
        preInsert(entity);
        this.commonUnitCostDAO.insert(entity);
        return CommonResultEnum.SUCCESS;
    }

    /**
     * 修改数据
     *
     * @param dto 实例对象
     * @return 操作结果
     */
    @Override
    public ServiceResult update(CommonUnitCostDTO dto) {
        String id = dto.getId();
        if (StringUtils.isEmpty(id)) {
            return CommonResultEnum.NOT_EXIST;
        }
        CommonUnitCost entity = this.commonUnitCostDAO.selectById(id);
        if (entity == null) {
            return CommonResultEnum.NOT_EXIST;
        }
        // TODO 添加其他校验逻辑，如数据权限

        entity.setVersionId(dto.getVersionId());
        entity.setCompanyId(dto.getCompanyId());
        entity.setOperateCostStandard(dto.getOperateCostStandard());
        entity.setMiningOperationPrice(dto.getMiningOperationPrice());
        entity.setWaterInjectionPrice(dto.getWaterInjectionPrice());
        entity.setHeavyOilThRecPrice(dto.getHeavyOilThRecPrice());
        entity.setOilGasProcessPrice(dto.getOilGasProcessPrice());
        entity.setCondensateRecyclePrice(dto.getCondensateRecyclePrice());
        entity.setUnderWellOperationPrice(dto.getUnderWellOperationPrice());
        entity.setWellTestPrice(dto.getWellTestPrice());
        entity.setGasPurificationPrice(dto.getGasPurificationPrice());
        entity.setRepairPrice(dto.getRepairPrice());
        entity.setTransferPrice(dto.getTransferPrice());
        entity.setOtherAuxiliaryPrice(dto.getOtherAuxiliaryPrice());
        entity.setFactoryMineManagePrice(dto.getFactoryMineManagePrice());
        this.commonUnitCostDAO.updateById(entity);
        return CommonResultEnum.SUCCESS;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 操作结果
     */
    @Override
    public ServiceResult deleteById(String id) {
        // TODO 添加校验逻辑，如数据权限
        this.commonUnitCostDAO.deleteById(id);
        return CommonResultEnum.SUCCESS;
    }

    @Override
    @Transactional
    public ServiceResult save(String versionId, List<CommonUnitCostDTO> dtoList) {
        if (dtoList == null || dtoList.isEmpty()) {
            return CommonResultEnum.SUCCESS;
        }
            this.deleteByVersion(versionId);
        List<CommonUnitCost> insertList = new ArrayList<>();
        for (CommonUnitCostDTO dto : dtoList) {
            CommonUnitCost entity = parseDTO2Entity(dto);
            preInsert(entity);
            entity.setVersionId(versionId);
            insertList.add(entity);
        }
        commonUnitCostDAO.insertBatch(insertList);
        return CommonResultEnum.SUCCESS;
    }

    @Override
    public ServiceResult deleteByVersion(String versionId) {
        QueryWrapper<CommonUnitCost> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CommonUnitCost::getVersionId, versionId);
        commonUnitCostDAO.delete(queryWrapper);
        return CommonResultEnum.SUCCESS;
    }

    @Override
    public List<CommonUnitCostVO> queryListByVersion(String commonParamVersionId) {

        CommonUnitCostDTO commonUnitCostDTO = new CommonUnitCostDTO();
        commonUnitCostDTO.setVersionId(commonParamVersionId);
        List<CommonUnitCostVO> commonUnitCostList = queryList(commonUnitCostDTO);
        return commonUnitCostList;
    }

    @Override
    public void copy(String fromId, String toId) {
        final CommonUnitCostDTO dto = new CommonUnitCostDTO();
        dto.setVersionId(fromId);
        final List<CommonUnitCost> commonUnitCosts = commonUnitCostDAO.queryList(dto);
        if (CollectionUtils.isEmpty(commonUnitCosts)) {
            return;
        }

        final List<CommonUnitCost> collect = commonUnitCosts.stream().map(m -> {
            final CommonUnitCost deepCopy = BusiUtil.deepCopy(m, CommonUnitCost.class);
            deepCopy.setId(IdGen.uuid());
            deepCopy.setCreateTime(new Date());
            deepCopy.setVersionId(toId);
            return deepCopy;
        }).collect(Collectors.toList());

        commonUnitCostDAO.insertBatch(collect);
    }


}
